package com.pig4cloud.pig.iot.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.iot.entity.IotDeviceMessageEntity;
import com.pig4cloud.pig.iot.service.IotDeviceMessageService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Objects;

/**
 * 设备预警消息表
 *
 * @author lty
 * @date 2025-03-12 20:11:35
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/iotDeviceMessage" )
@Tag(description = "iotDeviceMessage" , name = "设备预警消息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class IotDeviceMessageController {

    private final  IotDeviceMessageService iotDeviceMessageService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param iotDeviceMessage 设备预警消息表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('iot_iotDeviceMessage_view')")
	public R getIotDeviceMessagePage(@ParameterObject Page page, @ParameterObject IotDeviceMessageEntity iotDeviceMessage) {
		log.info("分页查询接口被调用，参数：page={}, iotDeviceMessage={}", page, iotDeviceMessage);
		LambdaQueryWrapper<IotDeviceMessageEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(iotDeviceMessage.getDevId()), IotDeviceMessageEntity::getDevId, iotDeviceMessage.getDevId());
		wrapper.eq(StrUtil.isNotBlank(iotDeviceMessage.getMessageType()), IotDeviceMessageEntity::getMessageType, iotDeviceMessage.getMessageType());
		wrapper.eq(StrUtil.isNotBlank(iotDeviceMessage.getMessageStatement()), IotDeviceMessageEntity::getMessageStatement, iotDeviceMessage.getMessageStatement());
		// 添加按创建时间降序排序
		wrapper.orderByDesc(IotDeviceMessageEntity::getCreateTime);
		// 如果没有createTime字段，可以使用id降序排序
		// wrapper.orderByDesc(IotDeviceMessageEntity::getId);
		return R.ok(iotDeviceMessageService.page(page, wrapper));
	}


    /**
     * 通过id查询设备预警消息表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceMessage_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(iotDeviceMessageService.getById(id));
    }

    /**
     * 新增设备预警消息表
     * @param iotDeviceMessage 设备预警消息表
     * @return R
     */
    @Operation(summary = "新增设备预警消息表" , description = "新增设备预警消息表" )
    @SysLog("新增设备预警消息表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceMessage_add')" )
    public R save(@RequestBody IotDeviceMessageEntity iotDeviceMessage) {
        return R.ok(iotDeviceMessageService.save(iotDeviceMessage));
    }

    /**
     * 修改设备预警消息表
     * @param iotDeviceMessage 设备预警消息表
     * @return R
     */
    @Operation(summary = "修改设备预警消息表" , description = "修改设备预警消息表" )
    @SysLog("修改设备预警消息表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceMessage_edit')" )
    public R updateById(@RequestBody IotDeviceMessageEntity iotDeviceMessage) {
        return R.ok(iotDeviceMessageService.updateById(iotDeviceMessage));
    }

    /**
     * 通过id删除设备预警消息表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除设备预警消息表" , description = "通过id删除设备预警消息表" )
    @SysLog("通过id删除设备预警消息表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceMessage_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(iotDeviceMessageService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param iotDeviceMessage 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceMessage_export')" )
    public List<IotDeviceMessageEntity> export(IotDeviceMessageEntity iotDeviceMessage,Long[] ids) {
        return iotDeviceMessageService.list(Wrappers.lambdaQuery(iotDeviceMessage).in(ArrayUtil.isNotEmpty(ids), IotDeviceMessageEntity::getId, ids));
    }

}